package cn.ltgodm.house.domain;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;

import java.util.HashMap;

/**
 * @author ltgodm
 * @date 2024-05-08 19:52:24
 */

public class ResultMap extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */

    public static final String CODE_TAG = "success";

    /**
     * 返回内容
     */

    public static final String MSG_TAG = "msg";

    /**
     * 数据对象
     */

    public static final String DATA_TAG = "data";

    public ResultMap() {
    }

    public ResultMap(Boolean success, String msg) {
        super.put(CODE_TAG, success);
        super.put(MSG_TAG, msg);
    }

    public ResultMap(Boolean success, String msg, Object data) {
        super.put(CODE_TAG, success);
        super.put(MSG_TAG, msg);
        if (ObjectUtil.isNotNull(data)) {
            super.put(DATA_TAG, data);
        }
    }

    public static ResultMap success() {
        return ResultMap.success("操作成功");
    }

    public static ResultMap success(String msg) {
        return ResultMap.success(msg, null);
    }

    public static ResultMap success(String msg, Object data) {
        return new ResultMap(true, msg, data);
    }

    public static ResultMap success(Object data) {
        return ResultMap.success("操作成功", data);
    }

    public static ResultMap error() {
        return ResultMap.error("操作失败");
    }

    public static ResultMap error(String msg) {
        return ResultMap.error(msg, null);
    }

    public static ResultMap error(String msg, Object data) {
        return new ResultMap(false, msg, data);
    }

    public static ResultMap error(int code, String msg) {
        return new ResultMap(false, msg);
    }

    public static ResultMap operate(int row) {
        return row > 0 ? ResultMap.success() : ResultMap.error();
    }

    /**
     * 方便链式调用
     *
     * @param key   键
     * @param value 值
     * @return 数据对象
     */
    @Override
    public ResultMap put(String key, Object value) {
        super.put(key, value);
        return this;
    }


}
